/**
 *  This code is a part of Quadratix project.
 * Copyright (C) 2009 - 2010 Valery A. Ilychev aka Sarutobi (ilychev at gmail dot com)
 * 
 *  This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package ru.sarutobi.quadratix;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import ru.sarutobi.quadratix.ui.BoardPanel;

public class AI {

	/** Вспомогательный объект анализа позиции */
	private Calculator calc;

	/** Игровая позиция */
	private int[] board;

	/** Размер игровой позиции */
	private int size;

	/** размер стороны доски */
	private int side;

	int movescounter;

	public void setCalc(Calculator calc){
		this.calc = calc;
	}

	public int move(int[] board, int move) {
		this.board = board;
		size = board.length;
		side = (int) Math.sqrt(size);

		calc.setSize(side);
		calc.setBoard(board);

		int score = Integer.MIN_VALUE;
		int best = 0;
		
		List<Integer> moves = new Vector<Integer>();
		
		for (int i = 0; i < size; i++) {
			if (board[i] == Board.EMPTY) {
				int tmp = calculateScore(i, best);
	
				if (tmp == score) {
					moves.add(i);
				}

				if (tmp > score) {
					moves.clear();
					moves.add(i);
					score = tmp;
				}
			}
		}

		best = (int) (Math.random() * moves.size());
		return moves.get(best);
	}

	private int positionCost(int forColor) {
		int[] scores = new int[board.length];
		for (int i = 0; i < board.length; i++){
			scores[i] = 0;
		}
		
		for (int i = 0; i < size; i++){
			if ((board[i] == Board.EMPTY) || (board[i] == forColor)){
				for (Iterator<int[]> squares = calc.getSquares(i).iterator(); squares.hasNext();){
					int[] square = squares.next();
					int mult = 0;
					boolean good = true;
					for (int j = 0; j < 4; j++){
						if ((board[square[j]] != forColor) && (board[square[j]] != Board.EMPTY)){
							good = false;
						}
						if (board[square[j]] == forColor){
							mult++;
						}
					}
					mult = (good) ? mult : 0;
					scores[i] += square[4] * mult;
				}
			}
		}
		int cost = 0;
		for (int i = 0; i < board.length; i++){
			cost += scores[i];
		}
	
		return cost;
	}

	private int calculateScore(final int pos, final int bound) {
		int whiteBefore = positionCost(Board.WHITE);
		int blackBefore = positionCost(Board.BLACK);
		board[pos] = Board.BLACK;
		int whiteAfter = positionCost(Board.WHITE);
		int blackAfter = positionCost(Board.BLACK);
		// System.out.println(String.format("After move %d white has %d change, black has %d change", pos, whiteAfter - whiteBefore, blackAfter - blackBefore));
		board[pos] = Board.EMPTY;
		return (blackAfter - blackBefore) - (whiteAfter - whiteBefore);
		}
}
